<?php

!defined('IN_ROOT') && exit('Access Denied');

class linemodel {

    var $db;
    var $base;

    function linemodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."line WHERE id='$id'");
    }
	function get_prcie($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."lineprice WHERE id='$id'");
    }
	function get_prciebylid($lid) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."lineprice WHERE lid='$lid'");
    }
    function get_list($start=0,$limit=1000, $where='') {
        $linelist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."line` WHERE $where ORDER BY `id` DESC limit $start,$limit");
        while($line=$this->db->fetch_array($query)) {
            $line['user']=$this->db->result_first("SELECT username FROM `".DB_TABLEPRE."user` where uid='$line[uid]'");
            $line['time'] = tdate($line['addtime']);
            $linelist[]=$line;
        }
        return $linelist;
    }
    function get_list_byuser($uid,$start=0,$limit=1000) {
        $linelist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."line` where uid='$uid' ORDER BY `addtime` ASC,`id` ASC limit $start,$limit");
        while($line=$this->db->fetch_array($query)) {
            $line['user']=$this->db->result_first("SELECT username FROM `".DB_TABLEPRE."user` where uid='$line[uid]'");
            $line['time'] = tdate($line['addtime']);
            $linelist[]=$line;
        }
        return $linelist;
    }


    function add($uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec,$idcode='',$city='', $cateid='') {
    	$time = $this->base->time;
        $this->db->query('REPLACE INTO `'.DB_TABLEPRE."line`(`uid` ,`title` ,`earlier_date` ,`upareaid` ,`group_date` ,`daynum` ,`traffic` ,`pics` ,`orderid` ,`isrec` ,`addtime`,`idcode`,`city`, `cateid`) values ('$uid','$title','$earlier_date','$pid','$group_date','$daynum','$traffic','$pics','$orderid','$isrec','$time','$idcode','$city', '$cateid')");
        return $this->db->insert_id();
    }

    function update($lid,$uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec,$idcode='',$city='') {
        $this->db->query('UPDATE  `'.DB_TABLEPRE."line`  set `title`='$title',`earlier_date`='$earlier_date',`pid`='$pid',`group_date`='$group_date',`daynum`='$daynum',`traffic`='$traffic',`pics`='$pics',`orderid`='$orderid',`isrec`='$isrec',`idcode`='$idcode',`city`='$city' where id='$lid' ");
    }
    function addprice($data) {
    	if ($data) {
    		return $this->db->insert(DB_TABLEPRE.'lineprice', $data);
    	} else {
    		return false;
    	}
    }
    
    function upprice($data, $id)
    {
    	$where = " `id`='$id'";
    	if ($data && $id) {
    		return $this->db->update(DB_TABLEPRE."lineprice", $data, $where);
    	} else {
    		return false;
    	}
    }
    
	function getpricelist($lid,$start=0,$limit=1000) {
        $linelist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."lineprice` ORDER BY `views` ASC,`id` ASC limit $start,$limit");
        while($line=$this->db->fetch_array($query)) {
            $linelist[]=$line;
        }
        return $linelist;
    }
    
    function order_line($id,$order) {
        $this->db->query("UPDATE `".DB_TABLEPRE."line` SET 	`displayorder` = '{$order}' WHERE `id` = '{$id}'");
    }
    function del_price($id){
    	$this->db->query("DELETE FROM `".DB_TABLEPRE."lineprice` WHERE `id`='$id'");
    }
    function get_day($lid) {
    	return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."lineday WHERE lid='$lid'");
    }
	function get_days($lid) {
        $linelist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."linedays` where lid='$lid' ORDER BY `days` ASC");
        while($line=$this->db->fetch_array($query)) {
            $linelist[]=$line;
        }
        return $linelist;
    }
    function upday($lid,$descs,$type,$content) {
    	$id = (int)$this->db->result_first("SELECT lid FROM ".DB_TABLEPRE."lineday WHERE lid='$lid'");
    	if($id == 0) {
    		$this->db->query('REPLACE INTO `'.DB_TABLEPRE."lineday`(`lid` ,`descs` ,`type` ,`content`) values ('$lid','$descs','$type','$content')");
    	}else{
    		$this->db->query("UPDATE `".DB_TABLEPRE."lineday` SET 	`descs` = '{$descs}',`type` = '{$type}',`content` = '{$content}' WHERE `lid` = '{$lid}'");
    	}
		
    }
    function get_dayshow($lid,$num) {
    	$str = '';
    	$linelist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."linedays` where lid='$lid' ORDER BY `days` ASC");
        while($line=$this->db->fetch_array($query)) {
            $linelist[$line['days']]=$line;
        }
		for($i=1;$i<=$num;$i++) {
			$chi = @explode(',',$linelist[$i]['chi']);
			if(in_array('1',$chi)){
				$c1 = ' checked';
			}else{
				$c1 = '';
			}
			$chi = @explode(',',$linelist[$i]['chi']);
			if(in_array('2',$chi)){
				$c2 = ' checked';
			}else{
				$c2 = '';
			}$chi = @explode(',',$linelist[$i]['chi']);
			if(in_array('3',$chi)){
				$c3 = ' checked';
			}else{
				$c3 = '';
			}
    		$str .= '<dl><dt><b>第'.$i.'天：</b><em>&nbsp;</em></dt><dd><input type="hidden" name="att[days]['.$i.']" value="'.$i.'"/><input type="text" class="text text380" maxlength="30" name="att[title]['.$i.']" value="'.$linelist[$i]['title'].'"/></dd>
			</dl>
			<dl>
			<dt>行程内容：<em>&nbsp;</em></dt>
			<dd><textarea name="att[content]['.$i.']" style="width:520px;" rows="12">'.$linelist[$i]['content'].'</textarea>
				  <a href="#" onclick="var t=this.parentNode.getElementsByTagName(\'textarea\')[0];t.rows=t.rows+5;return false;" title="增加输入框的高度">+行数</a>
				  <a href="#" onclick="var t=this.parentNode.getElementsByTagName(\'textarea\')[0];t.rows=Math.max(5,t.rows-5);return false;" title="减小输入框的高度">-行数</a></dd>
			</dl>
			<dl class="hotel">
			<dt>用餐：<em>&nbsp;</em></dt><dd>
			<label><input name="att[chi]['.$i.'][]" class="checkbox" type="checkbox" value="1" '.$c1.'/>早餐</label>
			<label> <input name="att[chi]['.$i.'][]" type="checkbox" class="checkbox" value="2" '.$c2.'/>中餐 </label> 
			<label><input name="att[chi]['.$i.'][]" type="checkbox"  class="checkbox" value="3" '.$c3.'/>晚餐</label>
			</dd>
			</dl>
			<dl>
			<dt>住宿：<em>&nbsp;</em></dt><dd><input type="text" class="text text160" name="att[zhu]['.$i.']" value="'.$linelist[$i]['zhu'].'"/></dd>
			</dl>';
		}
		return $str;
    }
    function updays($lid,$att) {
		foreach ($att['days'] as $v) {
			$title = $att[title][$v];
			$content = $att[content][$v];
			$chi = @implode(",", $att[chi][$v]);
			$zhu = $att[zhu][$v];
    		$id = (int)$this->db->result_first("SELECT id FROM ".DB_TABLEPRE."linedays WHERE lid='$lid' and days='$v'");
    		if($id == 0) {
	    		$this->db->query('REPLACE INTO `'.DB_TABLEPRE."linedays`(`lid` ,`days` ,`title` ,`content`,`chi`,`zhu`) values ('$lid','$v','$title','$content','$chi','$zhu')");
	    	}else{
	    		$this->db->query("UPDATE `".DB_TABLEPRE."linedays` SET 	`title` = '{$title}',`content` = '{$content}',`chi` = '{$chi}',`zhu` = '{$zhu}' WHERE `lid` = '{$lid}' and `days`='{$v}'");
	    	}
		}
    }
    function adddesc($data) {
    	if ($data) {
    		return $this->db->insert(DB_TABLEPRE.'linedesc', $data);
    	} else {
    		return false;
    	}
    }
    function get_descs($lid) {
		return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."linedesc WHERE lid='$lid'");
    }
    function addcate($data) {
    	$table = DB_TABLEPRE . 'linecate';
    	if ($data) {
    		return $this->db->insert($table, $data);
    	} else {
    		return false;
    	}
    }
    function getcate($uid) {
    	$sql = "SELECT * FROM " . DB_TABLEPRE. "linecate WHERE `uid`='$uid' ORDER BY `cate_id` DESC";
    	return $this->db->fetch_all($sql);
    }
    
    function getcateone($cate_id) {
    	$result = $this->db->get_one("SELECT * FROM " . DB_TABLEPRE. "linecate WHERE `cate_id`='$cate_id'");
    	return $result;	
    }
    
    function deletecate($cate_id) {
    	$result = $this->db->query("DELETE FROM " . DB_TABLEPRE . "linecate WHERE `cate_id`='$cate_id'");
    }    
    
    function updescs($lid,$xztitle,$xzcontent,$tstitle,$tscontent) {
		$id = (int)$this->db->result_first("SELECT lid FROM ".DB_TABLEPRE."linedesc WHERE lid='$lid'");
    	if($id == 0) {
	    	$this->db->query('REPLACE INTO `'.DB_TABLEPRE."linedesc`(`lid` ,`xztitle` ,`xzcontent` ,`tstitle`,`tscontent`) values ('$lid','$xztitle','$xzcontent','$tstitle','$tscontent')");
	    }else{
	    	$this->db->query("UPDATE `".DB_TABLEPRE."linedesc` SET 	`xztitle` = '{$xztitle}',`xzcontent` = '{$xzcontent}',`tstitle` = '{$tstitle}',`tscontent` = '{$tscontent}' WHERE `lid` = '{$lid}'");
	    }
    }
    function remove_by_id($id) {
        $this->db->query("DELETE FROM `".DB_TABLEPRE."line` WHERE `id` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."lineday` WHERE `lid` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."linedays` WHERE `lid` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."linedesc` WHERE `lid` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."lineprice` WHERE `lid` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."mapline` WHERE `lid` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."maplinecate` WHERE `lid` = '$id'");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."maplineview` WHERE `lid` = '$id'");
    }
}
?>
